<script lang="ts" setup>
type FormInstance = any
type FormRules = Record<string, any>

interface Props {
  modelValue: boolean
}

interface Emits {
  (e: "update:modelValue", value: boolean): void
  (e: "confirm", data: CreateInvitationData): void
}

interface CreateInvitationData {
  agent: string
  productVersion: string
  quantity: number
  expiredDate: string
}

const props = defineProps<Props>()
const emit = defineEmits<Emits>()

const formRef = ref<FormInstance>()

// 表单数据
const formData = reactive<CreateInvitationData>({
  agent: "",
  productVersion: "",
  quantity: 1,
  expiredDate: ""
})

// 代理商选项
const agentOptions = ref([
  { label: "华东区域服务商 > 上海代理商A", value: "shanghai-a" },
  { label: "华东区域服务商 > 杭州代理商B", value: "hangzhou-b" },
  { label: "华南区域服务商 > 深圳代理商C", value: "shenzhen-c" },
  { label: "华北区域服务商 > 北京代理商D", value: "beijing-d" }
])

// 产品版本选项
const productVersionOptions = ref([
  { label: "标准版 (398元/账号/月)", value: "standard" },
  { label: "专业版 (598元/账号/月)", value: "professional" },
  { label: "企业版 (998元/账号/月)", value: "enterprise" },
  { label: "旗舰版 (1598元/账号/月)", value: "flagship" }
])

// 表单验证规则
const rules: FormRules = {
  agent: [
    { required: true, message: "请选择代理商", trigger: "change" }
  ],
  productVersion: [
    { required: true, message: "请选择产品版本", trigger: "change" }
  ],
  quantity: [
    { required: true, message: "请输入生成数量", trigger: "blur" },
    { type: "number", min: 1, max: 100, message: "生成数量必须在1-100之间", trigger: "blur" }
  ],
  expiredDate: [
    { required: true, message: "请选择有效期", trigger: "change" }
  ]
}

// 弹窗显示状态
const dialogVisible = computed({
  get: () => props.modelValue,
  set: (value: boolean) => emit("update:modelValue", value)
})

// 重置表单
function resetForm() {
  formData.agent = ""
  formData.productVersion = ""
  formData.quantity = 1
  formData.expiredDate = ""
  formRef.value?.clearValidate()
}

// 监听弹窗关闭，重置表单
watch(dialogVisible, (newVal: boolean) => {
  if (!newVal) {
    resetForm()
  }
})

// 确认生成
async function handleConfirm() {
  if (!formRef.value) return

  try {
    await formRef.value.validate()
    emit("confirm", { ...formData })
    dialogVisible.value = false
  } catch (error) {
    console.log("表单验证失败:", error)
  }
}

// 取消
function handleCancel() {
  dialogVisible.value = false
}
</script>

<template>
  <el-dialog
    v-model="dialogVisible"
    title="生成邀请码"
    width="500px"
    :close-on-click-modal="true"
    class="create-invitation-dialog"
    :destroy-on-close="true"
  >
    <div class="dialog-content">
      <p class="dialog-description">
        为指定代理商生成特定产品版本的邀请码，支持批量生成。
      </p>

      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="80px"
        class="create-form"
      >
        <el-form-item label="选择代理" prop="agent">
          <el-select
            v-model="formData.agent"
            placeholder="选择要分配邀请码的代理"
            style="width: 100%"
          >
            <el-option
              v-for="option in agentOptions"
              :key="option.value"
              :label="option.label"
              :value="option.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="产品版本" prop="productVersion">
          <el-select
            v-model="formData.productVersion"
            placeholder="选择产品版本"
            style="width: 100%"
          >
            <el-option
              v-for="option in productVersionOptions"
              :key="option.value"
              :label="option.label"
              :value="option.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="生成数量" prop="quantity">
          <el-input
            v-model.number="formData.quantity"
            type="number"
            :min="1"
            :max="100"
            placeholder="请输入生成数量"
            style="width: 100%"
          />
        </el-form-item>

        <el-form-item label="有效期" prop="expiredDate">
          <el-date-picker
            v-model="formData.expiredDate"
            type="date"
            placeholder="年/月/日"
            style="width: 100%"
            format="YYYY/MM/DD"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleCancel">取消</el-button>
        <el-button
          type="primary"
          @click="handleConfirm"
          class="confirm-btn"
        >
          生成邀请码
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style lang="scss" scoped>
.create-invitation-dialog {
  :deep(.el-dialog) {
    border-radius: 12px;
  }

  :deep(.el-dialog__header) {
    padding: 20px 24px 16px;
    border-bottom: 1px solid var(--el-border-color-extra-light);

    .el-dialog__title {
      font-size: 18px;
      font-weight: 600;
      color: var(--el-text-color-primary);
    }
  }

  :deep(.el-dialog__body) {
    padding: 20px 24px;
  }

  :deep(.el-dialog__footer) {
    padding: 16px 24px 20px;
    border-top: 1px solid var(--el-border-color-extra-light);
  }

  .dialog-content {
    .dialog-description {
      font-size: 14px;
      color: var(--el-text-color-secondary);
      margin: 0 0 20px 0;
      line-height: 1.5;
    }

    .create-form {
      :deep(.el-form-item) {
        margin-bottom: 20px;

        .el-form-item__label {
          font-size: 14px;
          color: var(--el-text-color-regular);
          font-weight: 500;
        }

        .el-form-item__content {
          height: 40px;
          display: flex;
          align-items: center;
        }

        .el-select,
        .el-input,
        .el-date-editor {
          height: 40px;
          width: 100%;

          .el-select__wrapper,
          .el-input__wrapper {
            border-radius: 6px;
            border: 0.5px solid var(--el-border-color-lighter);
            height: 40px;

            &:hover {
              border-color: var(--el-border-color);
            }

            &.is-focused {
              border-color: var(--el-color-primary);
              box-shadow: 0 0 0 2px var(--el-color-primary-light-9);
            }
          }

          .el-input__inner {
            font-size: 14px;
            height: 38px;
            line-height: 38px;
          }
        }
      }
    }
  }

  .dialog-footer {
    display: flex;
    justify-content: flex-end;
    gap: 12px;

    .el-button {
      padding: 8px 20px;
      font-size: 14px;
      border-radius: 6px;
      font-weight: 500;
      min-width: 80px;
    }

    .confirm-btn {
      background-color: var(--el-color-primary);
      border-color: var(--el-color-primary);
      color: var(--el-color-white);

      &:hover {
        background-color: var(--el-color-primary-dark-2);
        border-color: var(--el-color-primary-dark-2);
      }

      &:active {
        background-color: var(--el-color-primary);
        border-color: var(--el-color-primary);
      }
    }
  }
}
</style>
